package com.yz.android_baozou.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yz.android_baozou.R;
import com.yz.android_baozou.activity.MainPageDetailActivity;
import com.yz.android_baozou.bean.MainPageBean;
import com.yz.android_baozou.bean.UserSubmissionBean;
import com.yz.android_baozou.helper.BitmapCircleTransformation;
import com.yz.android_baozou.helper.RecyclerViewAdapterHelper;


import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

public class Home_MainPage_RecyclerAdapter extends RecyclerViewAdapterHelper<MainPageBean.DataBean> {
    //声明上下文对象
    private Context mContext;
    //声明实体类的集合
    private List<MainPageBean.DataBean> mList_DataBean;
    //声明类型的Type值
    private static final int VIHOLDER1 = 0;
    private static final int VIHOLDER2 = 1;
    //声明Recyleer对象
    private RecyclerView mRecyclerView;

    //构造方法
    public Home_MainPage_RecyclerAdapter(Context context, List<MainPageBean.DataBean> list, RecyclerView recyclerView) {
        super(context, list);
        mRecyclerView = recyclerView;
        this.mContext = context;
        this.mList_DataBean = list;
    }


    @Override
    //获得穿入的ViewHolder类型
    public int getItemViewType(int position) {
        if (mList_DataBean.get(position).getAuthor_avatar() == null) {
            return VIHOLDER2;
        } else {
            return VIHOLDER1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        //根据传入的ViewHolder类型初始化不同的实体类
        //根据布局管理器获得item布局
        View view = null;
        switch (viewType) {
            case VIHOLDER1:
                view = inflater.inflate(R.layout.item1_homepage_home, parent, false);
                //给view设置监听
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = mRecyclerView.getChildAdapterPosition(v);
                        String url = mList_DataBean.get(position).getShare_url();
                        Bundle bundle = new Bundle();
                        //获得item的url
                        bundle.putString("url", url);
                        //声明并且初始化intemt对象
                        Intent intent = new Intent(mContext, MainPageDetailActivity.class);
                        intent.putExtra("url", bundle);
                        //进行跳转
                        mContext.startActivity(intent);
                    }
                });
                return new ViewHolder1(view);
            case VIHOLDER2:
                view = inflater.inflate(R.layout.item2_homepage_home, parent, false);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = mRecyclerView.getChildAdapterPosition(v);
                        String url = mList_DataBean.get(position).getShare_url();
                        Bundle bundle = new Bundle();
                        bundle.putString("url", url);
                        Intent intent = new Intent(mContext, MainPageDetailActivity.class);
                        intent.putExtra("url", bundle);
                        mContext.startActivity(intent);
                    }
                });
                return new ViewHolder2(view);
        }
        return null;
    }

    @Override
    //绑定item布局
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        String content = mList_DataBean.get(position).getTitle();
        String name = mList_DataBean.get(position).getAuthor_name();
        String show = mList_DataBean.get(position).getThumbnail();
        if (holder instanceof ViewHolder1) {
            String userIcon = mList_DataBean.get(position).getAuthor_avatar();
            ((ViewHolder1) holder).textView_Content_item1.setText(content);
            ((ViewHolder1) holder).textView_author_item1.setText(name);
            Glide.with(mContext).load(show).into(((ViewHolder1) holder).imageView_show_item1);
            Glide.with(mContext).load(userIcon)
                    .transform(new BitmapCircleTransformation(mContext))
                    .into(((ViewHolder1) holder).imageView_Icon_item1);
        } else if (holder instanceof ViewHolder2) {
            ((ViewHolder2) holder).textView_Content_item2.setText(content);
            ((ViewHolder2) holder).textView_author_item2.setText(name);
            Glide.with(mContext).load(show).into(((ViewHolder2) holder).imageView_show_item2);
        }
    }

    //声明item1，实体类
    class ViewHolder1 extends RecyclerView.ViewHolder {
        private ImageView imageView_show_item1;
        private ImageView imageView_Icon_item1;
        private TextView textView_Content_item1;
        private TextView textView_author_item1;

        public ViewHolder1(View itemView) {
            super(itemView);
            imageView_Icon_item1 = (ImageView) itemView.findViewById(R.id.imageView_Icon_item1);
            imageView_show_item1 = (ImageView) itemView.findViewById(R.id.imageView_show_item1);
            textView_Content_item1 = (TextView) itemView.findViewById(R.id.textView_Content_item1);
            textView_author_item1 = (TextView) itemView.findViewById(R.id.textView_author_item1);
        }
    }

    //声明item1，实体类
    class ViewHolder2 extends RecyclerView.ViewHolder {
        private ImageView imageView_show_item2;
        private TextView textView_Content_item2;
        private TextView textView_author_item2;

        public ViewHolder2(View itemView) {
            super(itemView);
            imageView_show_item2 = (ImageView) itemView.findViewById(R.id.imageView_show_item2);
            textView_Content_item2 = (TextView) itemView.findViewById(R.id.textView_Content_item2);
            textView_author_item2 = (TextView) itemView.findViewById(R.id.textView_author_item2);
        }
    }

    //gosn根据json串解析成实体类
    public MainPageBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<MainPageBean>() {
        }.getType());
    }


}
